"""
@name : 1.上下文管理器基本语法
@author : wenyao
@projectname: sanchuanglianxi
"""
#普通方法
# myfile = open("contest","r")
# content = myfile.read()
# myfile.close()

#用with语句执行
# with open("contest","r") as f:
#     print(dir(f))
#     content = f.read()

#
class ContextManager():
    def __init__(self):
        self.entered = False

    def __enter__(self):
        print("__enter__ called")
        self.entered = True
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        print("__exit__ called")
        print(exc_type, exc_val, exc_tb)
        self.entered = False

#直接创建
# cm01 = ContextManager()
# print(cm01.entered)

#使用with方法创建
#with 语句 会先执行__enter__方法， 退出的时候自动执行__exit__
with ContextManager() as cm02:
    print(cm02.entered)

#正常退出 __exit__三个值都为None